/* 1455. 检查单词是否为句中其他单词的前缀  */
/**
 * @param {string} sentence
 * @param {string} searchWord
 * @return {number}
 */
var isPrefixOfWord = function (sentence, searchWord) {
	const arr = sentence.split(' ')
	const index = arr.findIndex((item) => item.startsWith(searchWord))
	return index === -1 ? -1 : index + 1
}
/* 双指针 */
var isPrefixOfWord = function (sentence, searchWord) {
	let n = sentence.length,
		index = 1,
		start = 0,
		end = 0

	while (start < n) {
		while (end < n && sentence[end] !== ' ') {
			end++
		}
		if (isPrefixOf(sentence, start, end, searchWord)) return index
		index++
		end++
		start = end
	}
	return -1
}
const isPrefixOf = (sentence, start, end, searchWord) => {
	for (let i = 0; i < searchWord.length; i++) {
		if (start + i >= end || sentence[start + i] !== searchWord[i])
			return false
	}
	return true
}
const result = isPrefixOfWord('i love eating burger', 'burg')
console.log('isPrefixOfWord :>>', result)
